//
//  MeDefaultViewController.m
//  HJZWeiBo
//
//  Created by 蒋晨成 on 16/5/29.
//  Copyright © 2016年 蒋晨成. All rights reserved.
//

#import "MeDefaultViewController.h"
#import "MeInfoView.h"
#import "DefaultInfoTableViewCell.h"
#import "PhotoTableViewCell.h"
#import "FriendsListViewController.h"
@interface MeDefaultViewController ()<UIScrollViewDelegate, WBHttpRequestDelegate, UITableViewDelegate, UITableViewDataSource, MeInfoButtonDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIImageView *headImageView;

@property (nonatomic, strong) NSMutableArray *defaultArray;
@property (nonatomic, strong) NSMutableArray *statusArray;
@property (nonatomic, strong) NSMutableArray *photoArray;
@property (nonatomic, assign) NSInteger flag;
@property (nonatomic, strong) UIView *moveView;




@end

@implementation MeDefaultViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _headImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, -10, kWidth, 280)];
    _headImageView.image = [UIImage imageNamed:@"me_bg.jpg"];
    
    [self.view addSubview:_headImageView];

    
    self.flag = 1;
    self.view.backgroundColor = COLOR(230, 230, 230, 1);
    
    
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight - 64) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    MeInfoView *headView = [[NSBundle mainBundle] loadNibNamed:@"MeInfoView" owner:nil options:nil].firstObject;
    headView.delegate = self;
    headView.frame = CGRectMake(0, 0, kWidth, 180);
    headView.user = _user;
    
    self.tableView.tableHeaderView = headView;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"DefaultInfoTableViewCell" bundle:nil] forCellReuseIdentifier:@"defaultInfoTableViewCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"PhotoTableViewCell" bundle:nil] forCellReuseIdentifier:@"PhotoTableViewCell"];
    
}




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (_flag == 0) {
        return 1;
    }else if (_flag == 1) {
        return 1;
    }else {
        return 1;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_flag == 1) {
        DefaultInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"defaultInfoTableViewCell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.user = _user;
        return cell;
    }else if (_flag == 0) {
        DefaultInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"defaultInfoTableViewCell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.user = _user;
        return cell;
    }else {
        PhotoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PhotoTableViewCell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.user = _user;
        return cell;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (_flag == 1) {
        return 150;
    }else if (_flag == 0) {
        return 150;
    }else {
        return 110;
    }
   
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 39)];
    
    UIButton *defaultButton = [UIButton buttonWithType:UIButtonTypeCustom];
    defaultButton.frame = CGRectMake(0, 0, kWidth / 3, 39);
    defaultButton.tag = 100;
    [defaultButton setTitle:@"主页" forState:UIControlStateNormal];
    [defaultButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [view addSubview:defaultButton];
    
    UIButton *statusButton = [UIButton buttonWithType:UIButtonTypeCustom];
    statusButton.frame = CGRectMake(kWidth / 3, 0, kWidth / 3, 39);
    statusButton.tag = 101;
    [statusButton setTitle:@"微博" forState:UIControlStateNormal];
    
    [statusButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [view addSubview:statusButton];
    
    UIButton *photoButton = [UIButton buttonWithType:UIButtonTypeCustom];
    photoButton.frame = CGRectMake(kWidth * 2 / 3 , 0, kWidth / 3, 39);
    photoButton.tag = 102;
    [photoButton setTitle:@"相册" forState:UIControlStateNormal];
    [photoButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [view addSubview:photoButton];
    view.backgroundColor = [UIColor whiteColor];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 39, kWidth, 1)];
    lineView.backgroundColor = COLOR(230, 230, 230, 1);
    [view addSubview:lineView];
    
    self.moveView = [[UIView alloc] initWithFrame:CGRectMake(0, 38, 50, 2)];
    
    self.moveView.backgroundColor = [UIColor orangeColor];
    [view addSubview:_moveView];
    
    [statusButton addTarget:self action:@selector(changeMoveView:) forControlEvents:UIControlEventTouchUpInside];
    [defaultButton addTarget:self action:@selector(changeMoveView:) forControlEvents:UIControlEventTouchUpInside];
    [photoButton addTarget:self action:@selector(changeMoveView:) forControlEvents:UIControlEventTouchUpInside];
    
    if (_flag == 0) {
        [defaultButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
         self.moveView.center = CGPointMake(defaultButton.center.x, 38);
    }else if (_flag == 1) {
        [statusButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
         self.moveView.center = CGPointMake(statusButton.center.x, 38);
    }else {
        [photoButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
         self.moveView.center = CGPointMake(photoButton.center.x, 38);
    }
    
    
    return view;
}

- (void)changeMoveView:(UIButton *)button {
    [UIView animateWithDuration:0.5 animations:^{
        self.moveView.center = CGPointMake(button.center.x, 38);
        
        for (UIView * view in button.superview.subviews) {
            if ([view isKindOfClass:[UIButton class]]) {
                UIButton *inButton = (UIButton *)view;
                [inButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            }
        }
        
        [button setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    } completion:^(BOOL finished) {
    
       
        self.flag = button.tag - 100;
        [self.tableView reloadData];
    }];
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 40;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if ([scrollView isEqual:_tableView]) {
        CGFloat offsetY = scrollView.contentOffset.y ;
        
        
        CGRect newFrame = self.headImageView.frame;
        if (offsetY >= 0) {
            newFrame.origin.y = -10 - offsetY;
            
        }else if (offsetY >= -100 && offsetY < 0) {
            newFrame.origin.y = -10 - offsetY / 10;
        }else {
            newFrame.origin.y = - 100 - offsetY;
        }
        
        self.headImageView.frame = newFrame;
    }
    
}



- (void)pushFollowingUser {
    FriendsListViewController *friendListVC = [[FriendsListViewController alloc] init];
    friendListVC.userID = _user.userID;
    friendListVC.count = [_user.friendsCount integerValue];
    friendListVC.userType = @"friend";
    [self.navigationController pushViewController:friendListVC animated:YES];
}

- (void)pushFollowByUser {
    FriendsListViewController *friendListVC = [[FriendsListViewController alloc] init];
    friendListVC.count = [_user.followersCount integerValue];
    friendListVC.userID = _user.userID;
    friendListVC.userType = @"follower";
    [self.navigationController pushViewController:friendListVC animated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
